Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
Generate "id" in a foreach with modal dialog in Laravel with js

Someone can help me with js?? I need that all the time that this foreach is executed create an "id" to open the diferents dialog modals for each posts. How i can implement my {{post->id}} in js and generate new "id" for each posts?

This all code are inside another foreach that list all the posts:

<button type="button" id="btn-open-modal">
    Open
</button>

<dialog id="modal">
    <table>
        @foreach ($post->likers as $user)
            <tr>

                <td>
                    <a href="{{ route('profile', ['id' => $user->id]) }}">
                        <img class="rounded-md" style="width:50px; height:50px;" 
                        src="{{ $user->profile_photo_url }}" alt="{{ $user->name }}" />
                    </a>
                </td>
                <td>
                    <a href="{{ route('profile', ['id' => $user->id]) }}">
                        {{$user->name}}
                    </a>
                </td> 
            </tr>
        @endforeach
    </table> 
    <button id="btn-close-modal" class="btn">
        Close
    </button>
</dialog>

<script>
  const btnOpenModal = document.querySelector("#btn-open-modal");
  const btnCloseModal = document.querySelector("#btn-close-modal");
  const modal = document.querySelector("#modal");

  btnOpenModal.addEventListener("click",()=>{
      modal.showModal();
  })

  btnCloseModal.addEventListener("click",()=>{
      modal.close();
  })

</script> 

My js is very bad I would appreciate any help. Thanks ๐Ÿ˜‰

about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

I have tried but I can't do it... If i put manually the post id especific it works like this +[id='12']

How can i pass this variable {{$post->id}} inside the script?

var id = $(this).data('id');
const btnOpenModal = document.querySelector("#btn-open-modal" + [id = '12']);
const btnCloseModal = document.querySelector("#btn-close-modal" + [id = '12']);
const modal = document.querySelector("#modal" + [id = '12']);

btnOpenModal.addEventListener("click", () => {
  modal.showModal();
})

btnCloseModal.addEventListener("click", () => {
  modal.close();
})
<button type="button" id="btn-open-modal{{$post->id}}">
    Open                                                    
</button>

about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!